package edu.bw.mapper;

import edu.bw.po.Order;
import edu.bw.po.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface OrderMapper {


    /**
     * 查询所有订单(延迟加载订单关联的用户信息)
     * //@One注解相当于<association></association>
     *
     * @return
     */
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_id", property = "user", javaType = User.class,
                    one = @One(select = "edu.bw.mapper.UserMapper.findById", fetchType = FetchType.LAZY))
    })
    @Select("SELECT id, user_id, number, createtime, note FROM mybatis02.`order`")
    List<Order> findOrderAndUserLazy();


    /**
     * 查询所有订单(延迟加载订单关联的用户信息以及订单关联的订单明细)
     *
     * @return
     */
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_id", property = "user", javaType = User.class,
                    one = @One(select = "edu.bw.mapper.UserMapper.findById", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "orderDetails", javaType = List.class,
                    many = @Many(select = "edu.bw.mapper.OrderDetailMapper.findOrderDetailByOrderId", fetchType = FetchType.LAZY))
    })
    @Select("SELECT id, user_id, number, createtime, note FROM mybatis02.`order`")
    List<Order> findOrdersAndOrderDetailLazy();


    /**
     * 根据用户id查询用户关联的订单
     *
     * @param userId 用户id
     * @return
     */
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(property = "orderDetails", javaType = List.class, column = "id",
                    many = @Many(select = "edu.bw.mapper.OrderDetailMapper.findOrderDetailByOrderId", fetchType = FetchType.LAZY))
    })
    @Select("SELECT id, user_id, number, createtime, note FROM mybatis02.`order` WHERE user_id = #{userId}")
    List<Order> findOrdersByUserId(int userId);
}
